package comtgl.demo.utils;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import jakarta.servlet.http.HttpServletResponse;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;

public class QRCodeGenerator {

    // 生成二维码并保存为文件
    public static void generateQRCodeImage(String text, int width, int height, String filePath) throws Exception {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height);
        Path path = FileSystems.getDefault().getPath(filePath);
        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
    }

    // 生成二维码并返回字节数组
    public static byte[] getQRCodeImage(String text, int width, int height) throws Exception {
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height);
        ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();
        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream);
        return pngOutputStream.toByteArray();
    }

    // 在Servlet中返回二维码图片流
    public static void writeQRCodeToResponse(String text, int width, int height, HttpServletResponse response) {
        try {
            byte[] imageBytes = getQRCodeImage(text, width, height);

            // 设置响应头
            response.setContentType("image/png");
            response.setContentLength(imageBytes.length);

            // 输出图片流
            response.getOutputStream().write(imageBytes);
            response.getOutputStream().flush();
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    public static void main(String[] args) {
        try {
            // 示例：生成二维码并保存为文件
            String qrCodeContent = "{\n" +
                    "  \"person\": {\n" +
                    "    \"name\": \"张三\",\n" +
                    "    \"age\": 30,\n" +
                    "    \"isStudent\": false,\n" +
                    "    \"hobbies\": [\"阅读\", \"旅行\", \"编程\"],\n" +
                    "    \"address\": {\n" +
                    "      \"city\": \"北京\",\n" +
                    "      \"district\": \"海淀区\",\n" +
                    "      \"detail\": \"科技园路88号\"\n" +
                    "    },\n" +
                    "    \"contact\": {\n" +
                    "      \"phone\": \"13800138000\",\n" +
                    "      \"email\": \"zhangsan@example.com\",\n" +
                    "      \"wechat\": null\n" +
                    "    },\n" +
                    "    \"scores\": [95.5, 88, 92.0]\n" +
                    "  },\n" +
                    "  \"company\": {\n" +
                    "    \"name\": \"未来科技有限公司\",\n" +
                    "    \"establishedYear\": 2015,\n" +
                    "    \"departments\": [\n" +
                    "      {\"name\": \"研发部\", \"employeeCount\": 50},\n" +
                    "      {\"name\": \"市场部\", \"employeeCount\": 20},\n" +
                    "      {\"name\": \"人力资源部\", \"employeeCount\": 5}\n" +
                    "    ],\n" +
                    "    \"isListed\": false\n" +
                    "  }\n" +
                    "}";
            int width = 300;
            int height = 300;
            String filePath = "qrcode.png";

            generateQRCodeImage(qrCodeContent, width, height, filePath);
            System.out.println("二维码已生成：" + new File(filePath).getAbsolutePath());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}